package com.atguigu.main.sort;

import sun.text.resources.ja.FormatData_ja;

import java.util.Arrays;

/**
 * 选择排序
 *
 * @Author 剧情再美终是戏
 * @Date 2019/12/25
 * @return
 * @Version 1.0
 **/
public class SelectSort {

    public static void main(String[] args) {
        int[] array2 = {1, 3, 5, 2, 4, 9, 8, -1, 6};
        System.out.println(Arrays.toString(SelectSort.selectSort(array2)));
    }

    public static int[] selectSort(int[] array) {
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < array.length - 1; i++) {
            int min = array[i];
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < min) {
                    min = array[j];
                    minIndex = j;
                }
            }
            // 判断是否需要交换
            if (minIndex != i) {
                array[minIndex] = array[i];
                array[i] = min;
            }
        }
        System.out.printf("选择排序算法\t耗时%dms\n", System.currentTimeMillis() - startTime);
        return array;
    }
}
